<template>
  <div class="bigScreen">
    <div class="banner">
      <div class="show1">SanTanNew购卡平台</div>
      <div class="show2">简单、快捷、全面的一站式购卡平台</div>
    </div>
    <div v-if="!showObj()">交易类型异常,请重选!</div>
    <div class="feature" style="margin-top: 1em" v-if="showObj()">
      <div class="feature_item_2" @click="writeInfo()">
        <template v-if="type === '1'">
          <div class="page-title">妙妙屋专业版密钥</div>
        </template>
        <template v-if="type === '0'">
          <div class="page-title">妙妙屋普通版密钥</div>
        </template>
        <el-form :model="form" style="margin-top: 15px" ref="formTable">
          <el-row>
            <el-col
              :span="20"
              style="marin-top: 10px; display: flex; flex-direction: column"
            >
              <el-form-item
                label="手机号码"
                label-width="80px"
                prop="phone"
                :rules="{
                  required: true,
                  message: '不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input
                  style="width: 16rem"
                  v-model="form.phone"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="购买数量"
                label-width="80px"
                prop="amount"
                :rules="{
                  required: true,
                  message: '不能为空',
                  trigger: 'blur',
                }"
              >
                <el-input
                  style="width: 16rem"
                  v-model="form.amount"
                  placeholder="请输入"
                  oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                  @blur="salaryChange"
                ></el-input>
              </el-form-item>
              <el-form-item label="总额" label-width="80px">
                <div class="ddje">
                  ¥{{ unitPrice*form.amount }}
                  <span style="margin-left: 10px">库存:{{ stock }}</span>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="payBox">
          <!-- <img
            class="item_iconfont_3"
            :src="require('../assets/img/wPay.png')"
          /> -->
          <el-button type="success" @click="buy">微信支付</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import bigScreen from "@/api/bigScreen";
export default {
  data() {
    return {
      writePayInfo: false,
      isPaying: false,
      stock: 0,
      unitPrice: 999,
      form: {
        amount: 10,
        phone: "",
        type: "",
      },
      type: -1,
      maxCount: 999
    };
  },
  mounted() {},
  async created() {
    this.getVoiceCount();
    this.form.type = this.$route.query.type;
    this.maxCount = this.$route.query.maxCount;
    this.form.amount = this.maxCount;
    this.type = this.$route.query.type;
    this.form.type = this.type;
    this.unitPrice = this.$route.query.price;
    this.$forceUpdate();
  },
  methods: {
    showObj() {
      if (this.type == 3 || this.type == 2) {
        return true;
      }
      return false;
    },
    salaryChange(e) {
      this.form.amount = e.target.value;
      this.$forceUpdate()
    },
    async getVoiceCount() {
      const res = await bigScreen.getVoiceList();
      this.stock = res.stock;
    },
    writeInfo() {
      if (this.stock <= 0) {
        this.$message({
          message: "没有库存咯！无法购买。",
          type: "warning",
        });
      } else {
        this.writePayInfo = true;
      }
    },
    buy() {
      var regExp = new RegExp("^1[3578]\\d{9}$");
      this.$refs["formTable"].validate((valid) => {
        if (valid) {
          //  if (parseInt(this.stock) < parseInt(this.form.amount)) {
          //   this.$message({
          //     message: "库存不足",
          //     type: "warning",
          //   });
          //   return;
          // }
          if (this.form.amount < this.maxCount) {
            this.$message({
              message: "批发数量不能少于"+this.maxCount+'!',
              type: "warning",
            });
            return;
          }
          if (!regExp.test(this.form.phone)) {
            this.$message({
              message: "请输入正确的手机号码",
              type: "warning",
            });
            return;
          }

          localStorage.setItem("phone", this.form.phone);
          localStorage.setItem("payType", this.form.type);
          this.form.payType = this.form.type
          this.$router.push({
            name: "pay",
            query: this.form,
          });
        } else {
          return false;
        }
      });
    },
  },
  beforeDestroy() {},
};
</script>

<style lang="scss" scoped>
.bigScreen {
  width: 90%;
  text-align: center;
  padding-top: 0em;
  margin: 0 auto;
  background-color: #f5f7fb !important;
  .banner {
    background-color: #4aac94;
    padding-top: 3em;
    padding-bottom: 3em;
    .show1 {
      margin: 0;
      font-weight: 400;
      color: #fff;
      font-size: 3rem;
    }
    .show2 {
      margin-top: 1.2em;
      margin-bottom: 0;
      font-weight: 400;
      color: #fff;
      font-size: 1.2rem;
    }
    .show3 {
      font-weight: 400;
      margin-top: 0.8em;
      color: #fff;
      font-size: 1.2rem;
      margin-bottom: 0;
    }
  }
  .feature {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-font-smoothing: antialiased;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.42857143;
    color: #333;
    text-align: center;
    box-sizing: border-box;
    margin-top: 1em;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    .feature_item {
      cursor: pointer;
      vertical-align: top;
      display: inline-block;
      width: 22em;
      margin: 1em 1em;
      background-color: #fff;
      padding: 0.5em 2.5em;
      padding-bottom: 2em;
      box-shadow: 0 3px 3px 0 rgb(0 0 0 / 5%);
    }
    .feature_item_2 {
      cursor: pointer;
      vertical-align: top;
      display: inline-block;
      width: 22em;
      margin: 1em 1em;
      background-color: #fff;
      padding: 0.5em 2.5em;
      padding-bottom: 2em;
      padding-top: 2em;
      box-shadow: 0 3px 3px 0 rgb(0 0 0 / 5%);
      .page-title {
        display: inline-block;
        position: relative;
        padding-right: 25px;
        padding-bottom: 15px;
        color: #a788e7;
        font-size: 1.4em;
        text-align: left;
      }
      .page-title-wx {
        display: inline-block;
        position: relative;
        padding-right: 25px;
        padding-bottom: 15px;
        color: #67c23a;
        font-size: 1.4em;
        text-align: left;
      }
      .page-item {
        font-size: 1em;
        color: grey;
        text-align: left;
      }
    }
    .item_iconfont_1 {
      width: 10rem;
      height: 10rem;
      margin-top: 1.5em;
    }

    .item_iconfont_2 {
      width: 3rem;
      height: 3rem;
      margin-top: 1.5em;
    }
    .item_iconfont_3 {
      width: 3rem;
      height: 3rem;
    }
    .item_iconfont {
      width: 5rem;
      height: 5rem;
      margin-top: 1.5em;
    }
    .title {
      margin-top: 0.6em;
      margin-bottom: 0.6em;
      font-size: 2.5rem;
      width: 100%;
    }
    .feature_item .content {
      line-height: 200%;
      font-size: 1rem;
      color: #444;
    }
  }
  a {
    color: #4aac94;
    text-decoration: none;
  }
}
h2 {
  display: block;
  font-size: 1.5em;
  margin-block-start: 0.83em;
  margin-block-end: 0.83em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
  font-weight: bold;
}
.titleSpecial {
  margin-top: 1.6em;
  margin-bottom: 0.6em;
  font-size: 2.5rem;
  width: 100%;
}
.ddje {
  width: 100%;
  text-align: left;
  color: red;
}
.payBox {
  justify-content: center;
  display: flex;
  align-items: center;
  button {
    display: flex;
    align-items: center;
  }
}
</style>